#Implementation Descriptives

load("Implementation Main Data.Rdata")

#Table 3 ####
data<-data[,c("sig_minor_war_mh", "amnest_init",
              "bin", "incompatibility",
              "powtran_init", "prisr_init", 
              "truth_init", "spline")]
colnames(data)<-c("Signatory War Recurrence",
                  "Amnesty Implementation",
                  "Any Substitutes", "Incompatibility",
                  "Power-sharing Implementation", 
                  "Prisoner Release Implementation",
                  "Truth and Reconciliation Implementation",
                  "Time")
tab<-stargazer(data.frame(data), digits=3, 
               omit.summary.stat = c("p25", "median", "p75"), nobs=T, median = T)
tab
write(tab, file="Table 3.tex")

#Figure A3####
data<-na.omit(data)
corrplot(cor(data), type = "upper", diag=F, method="square", col=viridis(200),  tl.col="black")

pdf("FigA3.pdf")
print(corrplot(cor(data), type = "upper", diag=F, method="square", col=viridis(200),  tl.col="black"))
dev.off()


#Figure A5 ####
load("Implementation Main Data.Rdata")
n<-1000
set.seed(1015)
coef<-matrix(NA, ncol=4)
data<-na.omit(data[,c("bin", "amn_type_init", "sig_minor_war_mh", "pam_caseid")])
pop<-unique(data$pam_caseid)

i<-1
model<-NA
while(nrow(na.omit(coef))<n){
  sample<-sample(pop, replace = T, size = length(pop))
  map<-sapply(sample, function(x) which(data[,"pam_caseid"]==x))
  boot.sample<-data[unlist(map),]
  model<-brglm(sig_minor_war_mh~bin*as.factor(amn_type_init), data=boot.sample, family="binomial")
  if(is.na(model)) next
  if(model$converged==F) next
  if(dim(table(is.na(coef(model))))>1) next
  coef<-rbind(coef,summary(margins(model, variables="amn_type_init", at=list(bin=c(0,1))))[,3])
  #cat("\r", i, "of", n) 
  i<-i+1
  model<-NA
}
save<-coef
coef<-cbind(coef, coef[,1]-coef[,3])
coef<-cbind(coef, coef[,2]-coef[,4])

m<-brglm(sig_minor_war_mh~as.factor(bin)*as.factor(amn_type_init), data=data)
margins(m, variables="amn_type_init", at=list(bin=c(0,1)))   

point<-summary(margins(m, variables="amn_type_init", at=list(bin=c(0,1))))[,3]
point<-c(point, point[1]-point[3])
point<-c(point, point[2]-point[4])

plot.dat<-cbind.data.frame(point, apply(na.omit(coef),2,sort)[25,],
                           apply(na.omit(coef),2,sort)[975,], c(1,1,2,2,3,3), c(0,1,0,1,0,1))

colnames(plot.dat)[2:5]<-c("lb", "ub", "level", "Substitutes")

plot.dat[,4]<-factor(plot.dat[,4], levels=c(1,2,3),
                     labels=c("Effect of Amnesty with Exemptions Implementation",
                              "Effect of Amnesty without Exemptions Implementation",
                              "Effect of Amnesty Exemptions"))

plot.dat[,5]<-factor(plot.dat[,5], levels=c(0,1), labels=c("No Substitutes", "Substitutes"))

plot.dat[,6]<-c(3,3,2,2,1,1)
names(plot.dat)[6]<-"order"

figA5<-ggplot(plot.dat, aes(y=point, x=reorder(level, order)))+
  coord_flip()+
  geom_point(size=3, position=position_dodge(width=.5))+
  geom_linerange(aes(ymin=lb, ymax=ub), position=position_dodge(width=.5))+
  geom_hline(yintercept=0)+
  xlab("")+
  ylab("Marginal Effect of Amnesty on War Recurrence")+
  facet_wrap(~Substitutes)+
  theme_bw()
figA5
pdf("FigA5.pdf", width=8, height=4)
print(figA5)
dev.off()


rm(list=setdiff(ls(), c("cluster_bootstrap", "int_plot")))
